<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class TweetsAction extends BaseApiAction {

	public function index() {
		die('非法参数');
	}

	//添加Tweet
	public function save() {
		//判断是否登录
		$this->auth();
		//获取当前属于哪个学校页面
		$data['school_id'] = $_POST['school_id'];
		//获取类型
		$data['cate_id'] = $_POST['cate_id'];
		empty($data['cate_id']) && $this->ajaxReturn('', '请选择所属类型!', -1);

		if ($data['cate_id'] == 1) {
			$data["ext"] = $_POST['sex'];
			//获取地点
			$data['location_id'] = intval($_POST['location_id']);
		}

		//获取内容
		$data['content'] = trim($_POST['content']);
		empty($data['content']) && $this->ajaxReturn('', '请填写内容!', -2);
		$data['content'] = text_to_html($data['content']);
		//获取所在地点 
//		if(empty ($_GET['location'])){
//			 $school_name = D('School')->where('school_id ='.$curSchool)->getField('school_name');
//			 $data['location'] = $school_name."某一处";
//		}else{
//			$data['location'] = $_GET['location'];
//		}
		//获取用户ID
		$data['user_id'] = $this->uid;
		//获取随即昵称
		$data['nickname'] = get_rand_name();
		//获取发表时间
		$data['create_time'] = time();
		$data['status'] = 1;
		$data['tweets_id'] = D('Tweets')->data($data)->add();

		//格式化数据
		if ($data['location_id'])
			$data['location_name'] = D("Location")->where("location_id=" . $data['location_id'])->getField("location_name");
		$data['create_time'] = "刚刚";
		$view = new View();
		$view->assign("tweets", $data);
		$data['title'] = $view->fetch(HTML_PATH . "/_tweets/_" . (int) $data['cate_id'] . ".html");

		if ($data['tweets_id']) {
			$this->ajaxReturn($data, '发表成功!', 1);
		} else {
			$this->ajaxReturn(null, '发表失败，请再试!', 0);
		}
	}

	//删除Tweet
	public function delete() {
		$this->auth();
		$tweets_id = (int) $_GET["tweets_id"];
		$tweets = D("Tweets")->where("tweets_id=" . $tweets_id . " and user_id=" . $this->uid)->delete();
		if ($tweets) {
			$this->ajaxReturn($tweets, "删除成功", 1);
		} else {
			$this->ajaxReturn(0, "您没有删除该条主题的权限", 0);
		}
	}

	//获取Tweet列表
	public function lists() {
		$id = $_GET["id"] ? $_GET["id"] : 0;
		$start = $_GET["start"] ? $_GET["start"] : 0;
		$limit = $_GET["limit"] ? $_GET["limit"] : 20;
		$climit = $_GET["climit"] ? $_GET["climit"] : 0;
		if ($id) {
			$data['tweets_id'] = $id;
		} else {
			if ($_GET["school_id"])
				$data['school_id'] = $_GET["school_id"];
			//选择显示类别
			if ((int) $_GET['cate_id'] > 0) {
				$data['cate_id'] = $_GET['cate_id'];
			}
			if ($start > 0) {
				$data['tweets_id'] = array('lt', $start);
			}
		}
		$Tweets = D('Tweets');
		$tweets = $Tweets->relation(array("comment", 'location'))  //数据库新建一个表tweets_category : cate_id , category
				->where($data)->order('tweets_id desc')
				->limit("0," . $limit)->select();
		//格式化数据
		for ($i = 0; $i < count($tweets); $i++) {
			if($tweets[$i]['status'] != 1){
				$tweets[$i]['content'] = "该主题不存在或已被删除";
			}
			$tweets[$i]['create_time'] = friendly_date($tweets[$i]['create_time'], 'normal');
			$comment = $tweets[$i]['comment'];
			if ($comment) {
				for ($n = 0; $n < count($comment); $n++) {
					if ($tweets[$i]['comment'][$n]["status"] == 0 && $tweets[$i]['comment'][$n]['user_id'] != $this->uid) {
						$tweets[$i]['comment'][$n]["content"] = "该评论已被删除！";
					}
					$tweets[$i]['comment'][$n]['floor'] = $n + 1;
					$tweets[$i]['comment'][$n]['create_time'] = friendly_date($comment[$n]['create_time'], "normal");
					if ((int) $tweets[$i]['comment'][$n]["parent_id"] != 0) {
						$tweets[$i]['comment'][$n]['content'] = preg_replace("/to (\d+F)\:/i", "to <abbr class=\"comment_object\" data-parent-id=\"" . $tweets[$i]['comment'][$n]["parent_id"] . "\" title=\"回复$1的评论\">$1</abbr>:", $tweets[$i]['comment'][$n]['content']);
					}
				}
				if ($climit != 0) {
					$tweets[$i]['comment'] = array_splice($tweets[$i]['comment'], -$climit);
				}
			}
			$view = new View();
			$view->assign("tweets", $tweets[$i]);
			$tweets[$i]['title'] = $view->fetch(HTML_PATH . "/_tweets/_" . (int) $tweets[$i]['cate_id'] . ".html");
		}
		$this->ajaxReturn($tweets, "", 1);
	}

}

?>
